package com.sky.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.Service.OrderService;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.Result;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "订单相关接口")
@RestController(value = "orderController")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("提交订单信息")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        System.out.println("ordersSubmitDTO:" + ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrderInfo(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @ApiOperation("订单支付接口")
    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }

    /**
     * 获取订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("获取订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable Long id) {
        System.out.println("id:" + id);
        OrderVO orderDetail = orderService.getOrderDetail(id);
        return Result.success(orderDetail);

    }

    @ApiOperation("获取历史订单")
    @GetMapping("/historyOrders")
    public Result<IPage<OrderVO>> getHistoryOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        System.out.println("ordersPageQueryDTO:" + ordersPageQueryDTO);
        IPage<OrderVO> historyOrders = orderService.getHistoryOrders(ordersPageQueryDTO);
        return Result.success(historyOrders);
    }


    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result<String> cancelOrder(@PathVariable Long id){
        System.out.println("id:" + id);
        orderService.cancelUserOrder(id);
        return Result.success("订单取消成功");
    }

}
